package com.google.code.dajly;

import java.net.URL;
import java.util.List;
import java.io.InputStreamReader;

import com.sun.syndication.feed.synd.*;
import com.sun.syndication.io.SyndFeedInput;

/**
 * It Reads and prints any RSS/Atom feed type.
 * <p>
 * @author Stefano "pigoz" Pigozzi
 *
 */
public class FeedTest {

    public static void main(String[] args) {
    	String strurl = new String("http://www.corriere.it/rss/ultimora.xml");
        boolean ok = false;
        if (true) {
            try {
//                URL feedUrl = new URL(args[0]);
            	URL feedUrl = new URL(strurl);

                SyndFeedInput input = new SyndFeedInput();
                SyndFeed feed = input.build(new InputStreamReader(feedUrl.openStream()));
                                
                System.out.println(
                		feed.getTitle() + "\n" +
                		feed.getDescription() + "\n" +
                		feed.getImage().getUrl()+ "\n");
                
                SyndEntryImpl _e;
                List<?> _l = feed.getEntries();
                
                for (int i=0 ; i < _l.size() ; i++){
                	_e = (SyndEntryImpl) _l.get(i);
                	
                	System.out.println(
                			_e.getTitle()+ "\n" +
                			_e.getDescription().getValue()+ "\n"
                			);
                	
                }

                ok = true;
            }
            catch (Exception ex) {
                ex.printStackTrace();
                System.out.println("ERROR: "+ex.getMessage());
            }
        }

        if (!ok) {
            System.out.println();
            System.out.println("FeedReader reads and prints any RSS/Atom feed type.");
            System.out.println("The first parameter must be the URL of the feed to read.");
            System.out.println();
        }
    }

}
